package com.advert.system.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class WXPayConfigImpl extends WXPayConfig {

    private byte[] certData;
    private static WXPayConfigImpl INSTANCE;

    private String appid;
    private String mchId;
    private String apiKey;
    private String cert;
    private String subAppId;
    private String subMchId;

    public WXPayConfigImpl(String appid, String mchId, String apiKey, String cert, String subAppId, String subMchId) throws Exception{
        String certPath = cert;
        this.appid = appid;
        this.mchId = mchId;
        this.apiKey = apiKey;
        this.cert = cert;
        this.subAppId = subAppId;
        this.subMchId = subMchId;

        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

//    public static WXPayConfigImpl getInstance() throws Exception{
//        if (INSTANCE == null) {
//            synchronized (WXPayConfigImpl.class) {
//                if (INSTANCE == null) {
//                    INSTANCE = new WXPayConfigImpl();
//                }
//            }
//        }
//        return INSTANCE;
//    }

    public String getAppID() {
        return this.appid;
    }

    public String getMchID() {
        return this.mchId;
    }

    public String getKey() {

        if (getMchID().equals("1610014709")) {
            apiKey = "kseptonzhangjun1103jbvvvczkmklxb";
        }
        return this.apiKey;
    }

    public String getSubAppId() {
        return this.subAppId;
    }

    public String getSubMchId() {
        return this.subMchId;
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }


    public int getHttpConnectTimeoutMs() {
        return 2000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }

    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }

    public String getAlternateDomain() {
        return "api2.mch.weixin.qq.com";
    }

    @Override
    public int getReportWorkerNum() {
        return 1;
    }

    @Override
    public int getReportBatchSize() {
        return 2;
    }

    @Override
    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }




}
